#!/bin/bash

# Script created to fix a problem with files where the average backtracked levels were shifted left 8 bits

DATE=`date +"%y_%m_%d_%H_%M_%S"`
TMP_FILE=/tmp/temp_fix_file_${DATE}_${RANDOM}

BC_SCALE=5

FILE=$1
MODIFYING="false"

rm $TMP_FILE

while read line
do

	if [[ "$line" == "Average backtracked levels:" ]]
	then
		MODIFYING="true"
	fi

	if [[ "$line" =~ "Total time in GPU: "[0-9]+("."[0-9]+)? ]]
	then
		MODIFYING="false"
	fi

	if [[ "$line" =~ "Block "[0-9]+", thread "[0-9]+": " ]]
	then
		if [[ "$MODIFYING" == "true" ]]
		then
			VALUE=`echo $line | cut -d " " -f 5`

			REST=`echo $line | cut -d ":" -f 1`
			REST="\t"$REST": "
			if [[ $VALUE == "not" ]]
			then
				NEW_LINE=$REST"not run"
			else
				VALUE=`echo "scale = ${BC_SCALE}; $VALUE/256" | bc`
				NEW_LINE=$REST"\t"$VALUE
			fi

			echo -e $NEW_LINE >> $TMP_FILE
		else
			echo -e "\t"$line >> $TMP_FILE

		fi
	else
			echo -e $line >> $TMP_FILE
	fi

done < $FILE

#mv $TMP_FILE $FILE
